package com.ydgk.jishuzhan.controller;

import com.ydgk.jishuzhan.beans.Blog;
import com.ydgk.jishuzhan.beans.Question;
import com.ydgk.jishuzhan.beans.Talk;
import com.ydgk.jishuzhan.service.BlogService;
import com.ydgk.jishuzhan.service.QuestionService;
import com.ydgk.jishuzhan.service.TalkService;
import com.ydgk.jishuzhan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Date;
import java.util.List;


@RequestMapping("/talk")
@Controller
public class TalkController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private TalkService talkService;

    @RequestMapping("/doAdd")
    public String doAdd(Talk talk, Model model){
        talk.setGoodTotal(0);
        talk.setSendTime(new Date(System.currentTimeMillis()));
        talkService.save(talk);
        List<Blog> blogs=blogService.findBlogs();
        List<Question> questions=questionService.findQuestions();
        List<Talk> talks=talkService.findTalks();
        System.out.println();
        model.addAttribute("blogs",blogs);
        model.addAttribute("questions",questions);
        model.addAttribute("talks",talks);
        return "user-home-for-me";
    }

    @RequestMapping("/toIndex")
    public String toIndex(Model model){
        List<Talk> talks = talkService.findTalks();
        model.addAttribute("talks",talks);
        return "talk";
    }

    @RequestMapping("/toTalk-detail")
    public String toDetail(Integer id,Model model){
        Talk talk = talkService.findById(id);
        model.addAttribute("talk",talk);
        return "talk-detail";
    }
}
